package com.xlongwei.accounts.controller;

import java.time.LocalDate;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Inject;
import org.noear.solon.test.SolonTest;

import com.xlongwei.accounts.App;
import com.xlongwei.accounts.view.Report;

import features.SqlTest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SolonTest(App.class)
public class RecordControllerTest {
    @Inject
    RecordController recordController;
    @Inject
    UserController userController;

    @Test
    public void amount() {
        Assertions.assertEquals("0.01", RecordController.yuan(1));
        Assertions.assertEquals("0.12", RecordController.yuan(12));
        Assertions.assertEquals("1.23", RecordController.yuan(123));
        Assertions.assertEquals("-0.01", RecordController.yuan(-1));
        Assertions.assertEquals("-0.12", RecordController.yuan(-12));
        Assertions.assertEquals("-1.23", RecordController.yuan(-123));
        Assertions.assertEquals("1.1", RecordController.yuan(110));
        Assertions.assertEquals("1", RecordController.yuan(100));
        Assertions.assertEquals(1, RecordController.fen("0.01"));
        Assertions.assertEquals(10, RecordController.fen("0.1"));
        Assertions.assertEquals(10, RecordController.fen("0.10"));
        Assertions.assertEquals(11, RecordController.fen("0.11"));
        Assertions.assertEquals(101, RecordController.fen("1.01"));
        Assertions.assertEquals(110, RecordController.fen("1.1"));
    }

    @Test
    public void report() throws Exception {
        SqlTest.testAndRollback(() -> {
            userController.login("18810761776", "123456");
            recordController.create(1, "100", null, null, 1, 1, 1, LocalDate.parse("2025-01-01"), null);
            recordController.create(9, "200", null, null, 2, 1, 1, LocalDate.parse("2025-02-01"), null);
            recordController.create(10, "300", null, null, 1, 2, 1, LocalDate.parse("2024-12-01"), null);
            recordController.create(11, "400", null, null, 2, 2, 1, LocalDate.parse("2024-11-01"), null);
            recordController.create(2, "50", null, null, 1, 1, 1, LocalDate.parse("2025-01-01"), null);//outcome
            recordController.create(13, "60", null, null, 2, 1, 1, LocalDate.parse("2025-02-01"), null);
            recordController.create(14, "70", null, null, 1, 2, 1, LocalDate.parse("2024-12-01"), null);
            recordController.create(15, "80", null, null, 2, 2, 1, LocalDate.parse("2024-11-01"), null);
            Report report = new Report();
            log.info("{}", ONode.load(recordController.report(report)).toJson());
            report.setGroupBy("ledger");
            log.info("{}", ONode.load(recordController.report(report)).toJson());
            report.setGroupBy("account");
            log.info("{}", ONode.load(recordController.report(report)).toJson());
            report.setGroupBy("member");
            log.info("{}", ONode.load(recordController.report(report)).toJson());
        });
    }
}
